from math import ceil
import pandas as pd
import glob


def split_writer(origin_name, out_name, split_num):
    origin_csv = pd.read_csv(origin_name, sep=",", encoding="utf-8")
    i = 0
    num = 0
    split_size = ceil(origin_csv.shape[0] / split_num)
    for i in range(0, split_num):
        out_file = out_name+str(i+1)+".csv"
        print("Splitting: "+out_file)
        if i != split_num:
            out_lines = origin_csv.iloc[num: num+split_size]
        else:
            out_lines = origin_csv.iloc[num:]
        out_lines.to_csv(out_file, index=False)
        num += split_size


def combine_writer(folder_name):
    all_files = glob.glob(folder_name + "/*.csv")
    all_dataframe = []
    for file in all_files:
        dataframe = pd.read_csv(file)
        all_dataframe.append(dataframe)
    dataframe_concat = pd.concat(all_dataframe, axis=0, ignore_index=True, sort=False)
    print("Combining: " + folder_name+"/combined.csv")
    dataframe_concat.to_csv(folder_name+"/combined.csv", index=False, encoding="utf-8")


if __name__ == "__main__":
    split_writer("./faker/data.csv", "./faker/split/split", 4)
    combine_writer("./faker/split")
